﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.PublicShow
{
    public partial class SelectFgLDUser : BasePage
    {
        private Sys_UserBLL sub = new Sys_UserBLL();
        private Qs_Base_SetBLL qbb = new Qs_Base_SetBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["mode"].ToString()))
                {
                    string nems = Request["mode"].ToString();
                    string SelectType = (Request["mode"].ToString() == "1" ? "单选" : "多选");
                }

                string name = "分管矿领导";
                string cid = "0";
                string unit = "【" + "标题" + "】";
                TreeNode t = new TreeNode(name + unit, cid);
                MyTree.Nodes.Add(t);
                BindUser(t);
                MyTree.ExpandDepth = 1;
            }
        }

        private void BindUser(TreeNode tn)
        {
            Qs_Base_Set model = qbb.GetModelByName("N_FGLDID");

            DataTable dataUser = sub.GetAllUser(@" AND USER_DEPARTMENT=" + model.Set_value); //公司直属部门
            TreeNode tr = null;
            if (dataUser != null)
            {
                for (int i = 0; i < dataUser.Rows.Count; i++)
                {
                    tr = new TreeNode();
                    tr.Value = dataUser.Rows[i]["USER_ID"].ToString();
                    tr.Text = dataUser.Rows[i]["USER_NAME"].ToString() + "【人员】";
                    tn.ChildNodes.Add(tr);
                }
            }
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            TreeNodeCollection tnc = MyTree.CheckedNodes;
            try
            {
                int count = 0;
                string Ids = "";
                string Name = "";
                string SelectType = (Request["mode"].ToString() == "1" ? "单选" : "多选");
                foreach (TreeNode item in tnc)
                {
                    if (item.Checked)
                    {
                        bool na0 = item.Text.Contains("【标题】");

                        if (na0 != true)
                        {
                            count += 1;
                            Name += item.Text.Substring(0, item.Text.IndexOf("【")) + ",";
                            Ids += item.Value + ",";
                        }
                    }
                }
                if (SelectType == "单选" && count != 1)
                {
                    MessageBox.Show(Page, "请选择一个用户");
                    return;
                }
                if (count <= 0)
                {
                    MessageBox.Show(Page, "请至少选择一个用户");
                    return;
                }
                Name = Ids.Substring(0, +Ids.LastIndexOf(',')) + "|" + Name.Substring(0, Name.LastIndexOf(','));
                MessageBox.ResponseScript(Page, "window.returnValue='" + Name + "';window.opener=null;window.close();");
                MessageBox.ResponseScript(Page, "if( window.opener=undefined) {  window.opener.returnValue='" + Name + "'; } else {  window.returnValue='" + Name + "' ;  window.opener=null;}  window.close();");
            }
            catch (Exception exp)
            {
                MessageBox.Show(Page, exp.Message.ToString());
                return;
            }
        }
    }
}